//
//  WNTabBar.swift
//  WNWeibo
//
//  Created by 风不会停息 on 2017/7/22.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit

protocol addButtonDelegate : NSObjectProtocol {
    
    func clickAddButtonDe()
}


class WNTabBar: UITabBar {
    

    var addDelegate : addButtonDelegate?
    
    //按钮点击闭包
    var addButtonClosuer : (()->())?
    
    private lazy var addButton : UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: #selector(clickAddButton), for: .touchUpInside)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
    button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add") , for: .normal)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted") , for: .highlighted)
        
        button.sizeToFit()
        
        return button
    
    }()
    
    
    @objc private func clickAddButton()
    {
        addButtonClosuer?()
        
        self.addDelegate?.clickAddButtonDe()
        
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        setupUI()

    }
    
    //   添加子视图
    func setupUI() {
        //  设置背景图片，解决push阴影问题
        backgroundImage = UIImage(named: "tabbar_background")
        addSubview(addButton)
    }

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        addButton.centerX = width * 0.5
        addButton.centerY = height * 0.5

        let buttonWidth = width / 5
        
        //  索引
        var index = 0

        for subView in subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.width = buttonWidth
                //  设置x坐标
                subView.x = CGFloat(index) * buttonWidth
                //  记录系统按钮的索引
                index += 1
                if index == 2 {
                    //  多加上一个按钮的宽度，隔开中间加号按钮的宽度
                    index += 1
                }

            }
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
